package obs;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ManyThread {
    public static void debug(Object o){
        String s = ""+Thread.currentThread().getName();

        System.out.println(s+" thread: "+o.toString());
    }

    public static List<MeiMeiEvent> meiMeiqueue= new ArrayList<>();
    public static SyncXiaoMing syncXiaoMing = new SyncXiaoMing();
    public static ReentrantLock meimeilock = new ReentrantLock();
    public static Condition meimeitag = meimeilock.newCondition();

    public static void main(String[] args) throws InterruptedException {

        new Thread(()-> {
            SyncMeiMei syncMeiMei = new SyncMeiMei();
            Scanner scanner = new Scanner(System.in);
            while (true){
                String s = scanner.nextLine();
                Integer r= 0;
                try {
                    r = Integer.parseInt(s);
                }catch (Exception e){
                    r=100;
                }
                meimeilock.lock();
                try{
                    // 事件
                    syncMeiMei.setAge(r);
                    meiMeiqueue.add(new MeiMeiEvent(r,System.currentTimeMillis()));
                    debug("妹妹年龄　"+syncMeiMei.getAge());
                    //通知notify
                    meimeitag.signal();
                }finally {
                    meimeilock.unlock();
                }
            }


        },"meimei").start();
        new Thread(()-> {
            while (true){
                MeiMeiEvent event = null;
                meimeilock.lock();
                try{
                    while (meiMeiqueue.size()==0){

                        meimeitag.await();
                    }
                    //回调函数
                    //xiaoming_age=meimei_age+8;
                     event= meiMeiqueue.remove(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    meimeilock.unlock();
                }
                update(event);
                debug("小明年龄　"+syncXiaoMing.getAge());
            }


        },"xiaoming").start();

        while (true){
            Thread.sleep(2000);
            System.out.println();
            meimeilock.lock();
            try{
                debug(meiMeiqueue);

            }finally {
                meimeilock.unlock();
            }
        }

//        System.out.println("end");
    }
    public static void update(MeiMeiEvent meimei){
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        syncXiaoMing.setAge(meimei.getAge()+8);
    }
}
class SyncMeiMei{

    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "SyncMeiMei{" +
                "age=" + age +
                '}';
    }
}
class MeiMeiEvent{
    private int age;
    private long time;

    public MeiMeiEvent(int age, long time) {
        this.age = age;
        this.time = time;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    @Override
    public String toString() {
        return "MeiMeiEvent{" +
                "age=" + age +
                ", time=" + time +
                '}';
    }
}
class SyncXiaoMing{
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "SyncXiaoMing{" +
                "age=" + age +
                '}';
    }
}
class SyncXiaoZhang{
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    @Override
    public String toString() {
        return "SyncXiaoZhang{" +
                "age=" + age +
                '}';
    }
}
class SyncXiaoZhangMum{
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "SyncXiaoZhangMum{" +
                "age=" + age +
                '}';
    }
}